var layer = layui.layer,
	form = layui.form,
	laytpl = layui.laytpl,
	laydate = layui.laydate,
	table = layui.table,
	$ = layui.jquery;

var id = 1;

$("#insert").click(insert);
$("#sel").click(sel);

function insert() {
	var index = layer.open({
		type: 1,
		title: '添加记录',
		content: $("#insert-html").html(),
		success: function() {
			$("#save").click(save);
			$("#close").click(close);
			laydate.render({
				elem: '#birth'
			});
		}
	});
	form.render();
}

function save() {
	var name = $("[name='name']").val();
	var sex = $("[name='sex']:checked").val();
	var birth = $("#birth").val();
	var hobbies = "";
	$("[name='hobby']:checked").each(function(i) {
		hobbies += ($(this).val() + " ");
	});
	var addr = $("[name='addr']:selected").val();
	var desc = $("[name='desc']").val();
	var data = {
		id: id,
		name: name,
		sex: sex,
		birth: birth,
		hobbies: hobbies,
		addr: addr,
		desc: desc
	};
	var html = $("#tradd").html();
	var temp = laytpl(html).render(data);
	$("#ttbody").append(temp);
	id++;
	close();
}

function close() {
	layer.close(layer.index);
}

function rander_table(id) {
	table.render({
		elem: '#d' + id,
		height: 312,
		page: true
	});
}

function del(id) {
	if (confirm('是否删除该记录?')) {
		$("#tr" + id).remove();
		layer.msg('删除成功', {
			icon: 1,
			time: 700
		}, function() {});
	}
}

function upd(id) {
	layer.open({
		type: 1,
		content: $("#insert-html").html(),
		title: "修改记录",
		area: ['450px', '420px'],
		success: function() {
			var name = $("#tdb" + id).text();
			var sex = $("#tdc" + id).text();
			var birth = $("#tdd" + id).text();
			var hobbies = $("#tde" + id).text();
			var addr = $("#tdf" + id).text();
			var desc = $("#tdg" + id).text();
			$("input[name='name']").val(name);
			$("input[name='sex']").val(sex);
			$("input[id='birth']").val(birth);
			$("input[name='hobby']").each(function(i) {
				if (hobbies.indexOf($(this).val()) > -1) {
					$(this).prop('checked');
				}
			});
			$("option[name='addr']").each(function(i) {
				if ($(this).val() == addr) {
					$(this).attr({
						selected: "selected"
					});
					return;
				}
			});
			$("textarea[name='desc']").val(desc);
			$("#close").click(close);
			$("#save").click({
				id: id
			}, upd_save);
			laydate.render({
				elem: '#birth'
			});
		}
	});
	form.render();
}

function sel() {
	$("tbody tr").css({
		'background': ''
	});
	var name = $("[name='s-name']").val();
	var addr = $("[name='s-addr']:selected").val();
	if (name == '' && addr == '') {
		layer.msg('请输入有效的查询信息', {
			icon: 1,
			time: 1000
		}, function() {});
		return;
	}
	var trs = $("tr[id^=tr]");
	trs.each(function(i) {
		// Methods-1
		var a = (($(this).find('td[id^=tdb]').text().indexOf(name) > -1) || name == "");
		var b = (($(this).find('td[id^=tdf]').text().indexOf(addr) > -1) || addr == "");
		if (a && b) {
			$(this).css({
				"background": "red"
			});
		}
	});
}

function upd_save(event) {
	console.log(event);
	var id = event.data.id;
	console.log(id);
	var name = $("[name='name']").val();
	var sex = $("[name='sex']:checked").val();
	var birth = $("#birth").val();
	var hobbies = "";
	$("[name='hobby']:checked").each(function(i) {
		hobbies += ($(this).val() + " ");
	});
	var addr = $("[name='addr']:selected").val();
	var desc = $("[name='desc']").val();
	var data = {
		id: id,
		name: name,
		sex: sex,
		birth: birth,
		hobbies: hobbies,
		addr: addr,
		desc: desc
	};
	var html = $("#tdd").html();
	var temp = laytpl(html).render(data);
	console.log(temp);
	$("#tr" + id).html(temp);
	close();
}
